package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.repository.dao.mapper.UserBindsMapper;
import com.yingsheng.bi.hks.repository.dao.po.UserBinds;
import com.yingsheng.bi.hks.repository.dao.po.UserBindsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Repository
public class UserBindsDaoService implements DaoService {

    @Autowired
    private UserBindsMapper userBindsMapper;

    public UserBinds query(@NotNull String channel, @NotNull String openId) {
        UserBindsExample example = new UserBindsExample();
        example.createCriteria().andBindIdEqualTo(openId).andChannelIdEqualTo(channel);
        List<UserBinds> bindsList = userBindsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(bindsList) ? null : bindsList.get(0);
    }

    public void insert(@NotNull String channel, @NotNull String openId, @NotNull String userId) {
        UserBinds userBinds = new UserBinds();
        userBinds.setId(Utils.genUUid());
        userBinds.setUserId(userId);
        userBinds.setBindId(openId);
        userBinds.setChannelId(channel);
        userBinds.setBindState(EnumConst.State.A);
        userBinds.setCreateTime(new Date());
        userBindsMapper.insertSelective(userBinds);
    }

    public void update(@NotNull String channel, @NotNull String openId, @NotNull String userId) {
        UserBinds userBinds = new UserBinds();
        userBinds.setUserId(userId);
        UserBindsExample example = new UserBindsExample();
        example.createCriteria().andBindIdEqualTo(openId).andChannelIdEqualTo(channel).andUserIdNotEqualTo(userId);
        userBindsMapper.updateByExampleSelective(userBinds, example);
    }

}
